﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.Core;

namespace EPiServer.Municipality.Web.UI.WebControls
{
    public class UtilBar : WebControl {
        /* *******************************************************************
        *  Properties 
        * *******************************************************************/
        #region private PageData CurrentPage
        /// <summary>
        /// Gets the CurrentPage of the UtilBar
        /// </summary>
        /// <value></value>
        private PageData CurrentPage {
            get { return Page is PageBase ? ((PageBase)Page).CurrentPage : null; }
        }
        #endregion
        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
        #region protected override void Render(HtmlTextWriter writer)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer) {
            if (CurrentPage == null)
                return;

            base.Render(writer);
        }
        #endregion
        #region public override void RenderBeginTag(HtmlTextWriter writer)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderBeginTag(HtmlTextWriter writer) {
            writer.WriteBeginTag("div");
            writer.WriteAttribute("id", "meridium-utilbar");
            writer.Write(HtmlTextWriter.TagRightChar);
        }
        #endregion
        #region protected override void RenderContents(HtmlTextWriter writer)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(HtmlTextWriter writer) {
            if (!HttpContext.Current.User.Identity.IsAuthenticated || CurrentPage.PageLink.WorkID != 0) {
                return;
            }
            writer.WriteFullBeginTag("ul");
            //Admin
            if (Security.PrincipalInfo.HasAdminAccess) {
                writer.WriteFullBeginTag("li");
                writer.WriteBeginTag("a");
                writer.WriteAttribute("href", UriSupport.UIUrl + "admin/default.aspx");
                writer.WriteAttribute("class", "admin");
                writer.WriteAttribute("title", "Admin mode");
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.WriteLine("Administrate");
                writer.WriteEndTag("a");
                writer.WriteEndTag("li");
            }
            //Edit
            if (Security.PrincipalInfo.HasEditAccess) {
                writer.WriteFullBeginTag("li");
                writer.WriteBeginTag("a");
                writer.WriteAttribute("href", UriSupport.UIUrl + "edit/default.aspx?id=" + CurrentPage.PageLink);
                writer.WriteAttribute("class", "edit");
                writer.WriteAttribute("title", "Edit mode");
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.WriteLine("Edit");
                writer.WriteEndTag("a");
                writer.WriteEndTag("li");
            }
            writer.WriteEndTag("ul");
            writer.WriteBeginTag("a");
            writer.WriteAttribute("href", "#");
            writer.WriteAttribute("class", "close");
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.WriteLine("Close");
            writer.WriteEndTag("a");
        }
        #endregion
        #region public override void RenderEndTag(HtmlTextWriter writer)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderEndTag(HtmlTextWriter writer) {
            writer.WriteEndTag("div");
        }
        #endregion
    }
}